/*
 * 文 件 名 : ChatRecordDAO.java
 * 版    权 : China Machi.Co.,Ltd.Copyright 2009-2011.All rights reserved
 * 描    述 : <描述>
 * 修 改 人 : lixg
 * 修改时间 : 2012-9-18
 * 跟踪单号 : <跟踪单号>
 * 修改单号 : <修改单号>
 * 修改内容 : <修改内容>
 */
package com.fire8.dolphin.db;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.fire8.dolphin.model.ChatRecord;

/**
 * 好友列表操作类
 * <功能详细描述>
 *
 * @author  lixg
 * @version [版本号,2012-9-18]
 * @see     [相关类/方法]
 * @since   [产品/模块]
 */
public class ChatDAO
{
    private LixgDBHelper dbHelper;
    
    public ChatDAO(Context context)
    {
        dbHelper = new LixgDBHelper(context);
    }
    public  void add(ChatRecord chat)
    {
        ContentValues values = new ContentValues();
        
        values.put(ChatRecord.C_USER_ID, chat.getUserId());
        values.put(ChatRecord.C_ACCOUNT, chat.getAccount());
        values.put(ChatRecord.C_CHAT_ITEM, chat.getChatItem());
        values.put(ChatRecord.C_ITEM_TYPE, chat.getItemType());
        values.put(ChatRecord.C_CHAT_DATE, chat.getChatDate());
        values.put(ChatRecord.C_ITEM_IO, chat.getItemIO());
        dbHelper.getWritableDatabase().insert(ChatRecord.TABLE_NAME, null, values );
        dbHelper.close();
    }
    
    public HashMap<String, String> getObject(String id)
    {
        List<String> args = new ArrayList<String>();
        StringBuffer sql = new StringBuffer("SELECT ");
        sql.append(ChatRecord.C_CR_ID)
        .append(",").append(ChatRecord.C_USER_ID)
        .append(",").append(ChatRecord.C_ACCOUNT)
        .append(",").append(ChatRecord.C_CHAT_ITEM)
        .append(",").append(ChatRecord.C_ITEM_TYPE)
        .append(",").append(ChatRecord.C_CHAT_DATE)
        .append(",").append(ChatRecord.C_ITEM_IO)
        .append(" FROM ")
        .append(ChatRecord.TABLE_NAME)
        .append(" where 1=1 ");
        if(id != null)
        {
            sql.append(" and ").append(ChatRecord.C_CR_ID).append("=?");
            args.add(id);
        }
        Cursor cursor = dbHelper.getReadableDatabase().rawQuery(sql.toString(), args.toArray(new String[]{}));
        List<HashMap<String, String>> list = new ArrayList<HashMap<String,String>>();
        dbHelper.cursorToHashMap(cursor, list);
        if(cursor != null)
        {
            cursor.close();
            cursor = null;
        }
        dbHelper.close();
        if(!list.isEmpty())
        {
            return list.get(0);
        }
        return null;
    }
    
    public List<HashMap<String, String>> findList(ChatRecord chat)
    {
        List<String> args = new ArrayList<String>();
        StringBuffer sql = new StringBuffer("SELECT ");
        sql.append(ChatRecord.C_CR_ID)
        .append(",").append(ChatRecord.C_USER_ID)
        .append(",").append(ChatRecord.C_ACCOUNT)
        .append(",").append(ChatRecord.C_CHAT_ITEM)
        .append(",").append(ChatRecord.C_ITEM_TYPE)
        .append(",").append(ChatRecord.C_CHAT_DATE)
        .append(",").append(ChatRecord.C_ITEM_IO)
        .append(" FROM ")
        .append(ChatRecord.TABLE_NAME)
        .append(" where 1=1 ");
        if(chat != null )
        {
            if(chat.getUserId() != null)
            {
                sql.append(" and ").append(ChatRecord.C_USER_ID).append("=?");
                args.add(chat.getUserId().toString());
            }
            if(chat.getAccount() != null)
            {
                sql.append(" and ").append(ChatRecord.C_ACCOUNT).append("=?");
                args.add(chat.getAccount());
            }
        }
        
        Cursor cursor = dbHelper.getReadableDatabase().rawQuery(sql.toString(), args.toArray(new String[]{}));
        List<HashMap<String, String>> list = new ArrayList<HashMap<String,String>>();
        dbHelper.cursorToHashMap(cursor, list);
        if(cursor != null)
        {
            cursor.close();
            cursor = null;
        }
        dbHelper.close();
        return list;
    }
    
    public void delete(String account)
    {
        String[] args = {account};
        dbHelper.getWritableDatabase().delete(ChatRecord.TABLE_NAME, ChatRecord.C_ACCOUNT+"=? ", args );
    }
}
